

import { Controller } from 'egg';

export default class PlayListController extends Controller {
    public async index() {
        const { ctx } = this;
        ctx.body = await ctx.service.test.sayHi('egg');
    }

    public async update() {
        const { id, desc, tags, name } = this.ctx.query || this.ctx.body;

        console.log('update', id, desc, tags, name)
        const data = {
            "/api/playlist/desc/update": `{id:${id},desc:${desc}}`,
            "/api/playlist/tags/update": `{id:${id},tags:${tags}}`,
            "/api/playlist/update/name": `{id:${id},name:${name}}`,
            csrf_token: ""
        };

        const url = `/weapi/batch`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;

        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {

            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;

    }

    // /playlist/catlist
    public async catlist() {
        const url = `/weapi/playlist/catalogue`;
        const data = { csrf_token: "" }
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;

        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {

            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;

    }
    // /playlist/hot
    public async hotlist() {
        const url = `/weapi/playlist/hottags`;
        const data = { csrf_token: "" }
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;

        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {

            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        // console.log(body)
        this.ctx.body = body;
    }

    // /playlist/top  网友精选碟
    public async top() {
        const { cat, order, offset, limit, total } = this.ctx.query;
        const data = {
            cat: cat || "全部",
            order: order || "hot",
            offset: offset || 0,
            total: total ? "true" : "false",
            limit: limit || 50
        };

        const url = `/weapi/playlist/list`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;

        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {

            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;
    }

    // playlist/highquality
    public async highquality(){
        const {cat,offset,limit}=this.ctx.query;
        const data ={
            cat: cat || "全部",
            offset: offset || 0,
            limit: limit || 20,
        }
        const url = `/weapi/playlist/highquality/list`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;

        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {

            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;
    }

    // playlist/detail   id = 2025279629
    public async detail(){
        const {id}=this.ctx.query;
        const data = {csrf_token: "",id};
        const url = `/weapi/v3/playlist/detail`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;

        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {

            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;
    }

    // playlist/create
    public async create(){
        const {name}=this.ctx.query;
        const data = {name,csrf_token:''};
        const url = `/weapi/playlist/create`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body } = result;

        if(body.includes('illegal request!')){
            this.ctx.body = this.config.errmsg;
            this.ctx.status = 403
        }else{
            this.ctx.body=body;
        }

    }
    // playlist/subscribe?id=2065883262&type=1  再多的歌回不到从前
    public async subscribe(){
        const {type,id}=this.ctx.query;
        const data = {id,csrf_token:''};
        const action = type == 1?'subscribe':'unsubscribe';


        const url = `/weapi/playlist/${action}`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body } = result;
        this.ctx.body = body;
    }

    // playlist/track?op=del&pid=31607870&tracks=566443171,426290517

    public async track(){
        const {op,pid,tracks}=this.ctx.query;
        const data ={op,pid,trackIds:`[${tracks}]`};
        const url = `/weapi/playlist/manipulate/tracks`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body } = result;
        this.ctx.body = body;
    }

    


    

}